package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.AskForLeave;

@Repository
public interface AskForLeaveDao extends JpaRepository<AskForLeave, Integer> {

    @Transactional
    @Modifying
    Integer deleteByAskId(Integer askId);

    List<AskForLeave> findByAskId(Integer askId);

    @Transactional
    @Modifying
    Integer deleteByAskIdIn(List<Integer> listId);

    /*
     * @Query(nativeQuery = true, value =
     * "SELECT u.user_Id as userId,u.account as userName,u.nickName \r\n" +
     * "from (users u left join user_role ur on u.user_Id = ur.user_Id )\r\n" +
     * "left join role r on r.role_Id = ur.role_Id\r\n" +
     * "where r.roleName = '学生'  ")
     */
    @Query(nativeQuery = true, value = "SELECT u.user_Id,u.account as userName,u.nickName,ct.typeName,c.className,termName \r\n" +
            "FROM (((users u INNER JOIN student_class sc ON u.user_Id = sc.user_Id)\r\n" +
            "INNER JOIN classes c ON c.class_Id = sc.class_Id)\r\n" +
            "INNER JOIN class_type ct ON ct.class_type_Id = c.class_type_Id)\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id")
    List<Map<String, Object>> queryAllStudentInfo();

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,afl.* \r\n" +
            "from users u INNER JOIN ask_for_leave afl on u.user_Id = afl.user_Id")
    List<Map<String, Object>> queryAllStudentAndAskForLeaveInfo();

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,afl.* \r\n" +
            "from users u INNER JOIN ask_for_leave afl on u.user_Id = afl.user_Id\r\n" +
            "where afl.duration LIKE CONCAT('%',:duration,'%')")
    List<Map<String, Object>> findByDurationLike(@Param("duration") String duration);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,afl.* \r\n" +
            "from users u INNER JOIN ask_for_leave afl on u.user_Id = afl.user_Id\r\n" +
            "where afl.reason LIKE CONCAT('%',:reason,'%')")
    List<Map<String, Object>> findByReasonLike(@Param("reason") String reason);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,afl.* \r\n" +
            "from users u INNER JOIN ask_for_leave afl on u.user_Id = afl.user_Id\r\n" +
            "where afl.status LIKE CONCAT('%',:status,'%')")
    List<Map<String, Object>> findByStatusLike(@Param("status") String status);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,afl.* \r\n" +
            "from users u INNER JOIN ask_for_leave afl on u.user_Id = afl.user_Id\r\n" +
            "where u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "select u.account as userName,u.nickName,afl.* \r\n" +
            "from users u INNER JOIN ask_for_leave afl on u.user_Id = afl.user_Id\r\n" +
            "where u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

    @Query(nativeQuery = true, value = "SELECT u.account AS userName,u.nickName,afl.* \r\n" +
            "FROM users u INNER JOIN ask_for_leave afl ON u.user_Id = afl.user_Id\r\n" +
            "WHERE u.user_Id = :userId")
    List<Map<String, Object>> selectAskForLeaveByUserId(@Param("userId") Integer userId);


}
